<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class proyek_model extends CI_Model{
	
	function __construct(){
		parent::__construct();
		$this->load->database();
		}
		
	function add($proyek){
		$this->db->insert('proyek', $proyek);
		}
			
	function update($id_proyek, $proyek){
		$this->db->where('proyek.id_proyek', $id_proyek);
		$this->db->update('proyek', $proyek);
		}
		
	function delete($id_proyek){
		$this->db->where('proyek.id_proyek', $id_proyek);
		$this->db->delete('proyek');
		}
		
	function get_all($id_user){
		$this->db->select('*');
		$this->db->from('proyek');
		$this->db->join('sub_kategori_proyek', 'sub_kategori_proyek.id_sub_kategori_proyek = proyek.id_sub_kategori_proyek');
		$this->db->join('user_facebook', 'user_facebook.id_user = proyek.id_user');
		$this->db->join('status_proyek', 'status_proyek.id_status_proyek = proyek.id_status_proyek');
		$result = $this->db->get();
		return $result;
		}
		
	function get_by_user($id_user){
		$this->db->select('*');
		$this->db->from('proyek');
		$this->db->join('sub_kategori_proyek', 'sub_kategori_proyek.id_sub_kategori_proyek = proyek.id_sub_kategori_proyek');
		$this->db->join('user_facebook', 'user_facebook.id_user = proyek.id_user');
		$this->db->join('status_proyek', 'status_proyek.id_status_proyek = proyek.id_status_proyek');
		$this->db->where('proyek.id_user', $id_user);
		$result = $this->db->get();
		return $result;
		}
		
	function get_by_status($id_user,$id_status,$mode,$offset = 0, $limit = 1){
		$proyeks=array();
		$this->db->select('*');
		$this->db->from('proyek');
		$this->db->join('sub_kategori_proyek', 'sub_kategori_proyek.id_sub_kategori_proyek = proyek.id_sub_kategori_proyek');
		$this->db->join('user_facebook', 'user_facebook.id_user = proyek.id_user');
		$this->db->join('status_proyek', 'status_proyek.id_status_proyek = proyek.id_status_proyek');
		$this->db->where('proyek.id_user', $id_user);
		
		if($id_status!=0){
			$this->db->where('proyek.id_status_proyek', $id_status);
		}
		
		$this->db->limit($limit, $offset);
		$this->db->order_by('tanggal_pembuatan_proyek', $mode); 
		$results = $this->db->get();
		
		foreach($results->result() as $proyek)
		{
			$proyek->working_worker = $this->hasil_kerja($proyek->id_proyek);
			$proyek->unreviewed_work = $this->pekerjaan_belum_direview($proyek->id_proyek);
			$proyeks[]=$proyek;
		}
		return $proyeks;
	}
	
	function count_by_status($id_user,$id_status){
		$this->db->select('*');
		$this->db->from('proyek');
		$this->db->join('sub_kategori_proyek', 'sub_kategori_proyek.id_sub_kategori_proyek = proyek.id_sub_kategori_proyek');
		$this->db->join('user_facebook', 'user_facebook.id_user = proyek.id_user');
		$this->db->join('status_proyek', 'status_proyek.id_status_proyek = proyek.id_status_proyek');
		$this->db->where('proyek.id_user', $id_user);
		$this->db->where('proyek.id_status_proyek', $id_status);
		$result = $this->db->count_all_results();
		return $result;
		}
		
		
	function jumlah_by_user($id_user){
		$this->db->select('count(*) as jumlah_proyek');
		$this->db->from('proyek');
		$this->db->join('sub_kategori_proyek', 'sub_kategori_proyek.id_sub_kategori_proyek = proyek.id_sub_kategori_proyek');
		$this->db->join('user_facebook', 'user_facebook.id_user = proyek.id_user');
		$this->db->join('status_proyek', 'status_proyek.id_status_proyek = proyek.id_status_proyek');
		$this->db->where('proyek.id_user', $id_user);
		$result = $this->db->get();
		return $result;
		}
	
	function hasil_kerja($id_proyek){
		$this->db->select('count(*) as hasil_kerja');
		$this->db->from('hasil_pekerjaan');
		$this->db->where('hasil_pekerjaan.id_proyek', $id_proyek);
		$this->db->where('hasil_pekerjaan.id_status_review', 2);
		$result = $this->db->get();
		return $result->row();
	}
	
	function pekerjaan_belum_direview($id_proyek){
		$this->db->select('count(*) as hasil_kerja');
		$this->db->from('hasil_pekerjaan');
		$this->db->where('hasil_pekerjaan.id_proyek', $id_proyek);
		$this->db->where('hasil_pekerjaan.id_status_review', 1);
		$result = $this->db->get();
		return $result->row();
	}
	
	function pekerjaan_ditolak($id_proyek){
		$this->db->select('count(*) as hasil_kerja');
		$this->db->from('hasil_pekerjaan');
		$this->db->where('hasil_pekerjaan.id_proyek', $id_proyek);
		$this->db->where('hasil_pekerjaan.id_status_review', 3);
		$result = $this->db->get();
		return $result->row();
	}
		
	function get_proyek($id_proyek){
		$this->db->select('*');
		$this->db->from('proyek');
		$this->db->join('sub_kategori_proyek', 'sub_kategori_proyek.id_sub_kategori_proyek = proyek.id_sub_kategori_proyek');
		$this->db->join('user_facebook', 'user_facebook.id_user = proyek.id_user');
		$this->db->join('status_proyek', 'status_proyek.id_status_proyek = proyek.id_status_proyek');
		$this->db->where('proyek.id_proyek', $id_proyek);
		$result = $this->db->get();
		$proyek = $result->row();
		$proyek->kategori = $this->get_kategori($proyek->id_sub_kategori_proyek);
		$proyek->unreviewed_work = $this->pekerjaan_belum_direview($proyek->id_proyek);
		return $proyek;
	}

	function get_plain_proyek($id_proyek){
		$this->db->select('*');
		$this->db->from('proyek');
		$this->db->where('proyek.id_proyek', $id_proyek);		
		$result = $this->db->get();
		$proyek = $result->row();
		return $proyek;
	}
	
	//fungsi aneh, kayanya emang aneh, lupa buat apa.. hehehe :)
	function get_kategori($id_sub_kategori_proyek){
		$this->db->select('*');
		$this->db->from('kategori_proyek');	
		$this->db->join('sub_kategori_proyek', 'sub_kategori_proyek.id_kategori_proyek = kategori_proyek.id_kategori_proyek');
		$this->db->where('sub_kategori_proyek.id_sub_kategori_proyek', $id_sub_kategori_proyek);
		$result = $this->db->get();
		return $result->row();
	}
	
}
